package com.xjtu.duansheli.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjtu.duansheli.entity.ArticleCommentsInfo;
import com.xjtu.duansheli.entity.ProductCommentsInfo;
import com.xjtu.duansheli.exception.MyException;
import com.xjtu.duansheli.mapper.ArticleCommentsInfoMapper;
import com.xjtu.duansheli.service.ArticleCommentsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 漩涡鸣人
 * @since 2020-10-24
 */
@Service
public class ArticleCommentsInfoServiceImpl extends ServiceImpl<ArticleCommentsInfoMapper, ArticleCommentsInfo> implements ArticleCommentsInfoService {

    @Autowired
    private ArticleCommentsInfoMapper articleCommentsInfoMapper;

    @Override
    public List<ArticleCommentsInfo> getAllArticleComm(int current, int size) {
        Page<ArticleCommentsInfo> page = new Page<>(current,size);
        articleCommentsInfoMapper.selectPage(page, null);
        System.out.println(page.getTotal());
        return page.getRecords();
    }

    @Override
    public ArticleCommentsInfo getArticleCommByid(Long aid) {
        ArticleCommentsInfo articleCommentsInfo = articleCommentsInfoMapper.selectById(aid);
        if(articleCommentsInfo == null){
            throw new MyException(500,"不存在该帖子！");
        }
        return articleCommentsInfo;
    }

    @Override
    public int addArticleComm(ArticleCommentsInfo articleComm) {
        try{
            return articleCommentsInfoMapper.insert(articleComm);
        }catch (Exception e){
            throw new MyException(500,"添加帖子失败！");
        }
    }

    @Override
    public int updateArticleComm(ArticleCommentsInfo articleComm) {
        try{
            return articleCommentsInfoMapper.updateById(articleComm);
        }catch (Exception e){
            throw new MyException(500,"修改帖子失败！");
        }
    }

    @Override
    public int deleteArticleComm(Long aid) {
        try{
            return articleCommentsInfoMapper.deleteById(aid);
        }catch (Exception e){
            throw new MyException(500,"删除帖子失败！");
        }
    }

    @Override
    public List<ArticleCommentsInfo> getArticleComByuid(Long uid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("author_id",uid);
        List<ArticleCommentsInfo> articleCommentsInfos = articleCommentsInfoMapper.selectByMap(map);
        if(articleCommentsInfos.size() == 0){
            throw new MyException(500,"该用户目前未发表评论！");
        }
        return articleCommentsInfos;
    }

    @Override
    public List<ArticleCommentsInfo> getArticleComByaid(Long aid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("article_id",aid);
        List<ArticleCommentsInfo> articleCommentsInfos = articleCommentsInfoMapper.selectByMap(map);
        if(articleCommentsInfos.size() == 0){
            throw new MyException(500,"该帖子目前无评论！");
        }
        return articleCommentsInfos;
    }

    @Override
    public List<ArticleCommentsInfo> getArticleComByparentId(Long paid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("parent_id",paid);
        List<ArticleCommentsInfo> articleCommentsInfos = articleCommentsInfoMapper.selectByMap(map);
        if(articleCommentsInfos.size() == 0){
            throw new MyException(500,"该评论目前无子评论！");
        }
        return articleCommentsInfos;
    }

    @Override
    public int getTotal() {
        return articleCommentsInfoMapper.selectCount(null);
    }
}
